package com.javaear.beangenerator;

import com.javaear.beangenerator.tools.StringUtils;

/**
 * fielder model
 * with beaner can show a javaBean file content
 *
 * @author aooer
 */
public final class Fielder extends ContextHolder {

    private String fieldName;

    private String fieldType;

    public Fielder(String fieldName, String fieldType) {
        this.fieldName = StringUtils.lowerCaseFirstChar(fieldName);
        this.fieldType = StringUtils.upperCaseFirstChar(fieldType);
    }

    public void append(String prefixSpace, StringBuilder fieldBuilder, StringBuilder getterSetterBuilder) {
        this.appendField(prefixSpace, fieldBuilder);
        this.appendGetSet(prefixSpace, getterSetterBuilder);
    }

    public void appendField(String prefixSpace, StringBuilder fieldBuilder) {
        fieldBuilder.append(prefixSpace).append("private ").append(fieldType).append(" ").append(fieldName).append(";").append(StringUtils.LINE);
    }

    public void appendGetSet(String prefixSpace, StringBuilder getterSetterBuilder) {
        boolean enableCodeTemplate = context.getBoolean("config_code_template_enable");
        String getCodeTemp = StringUtils.LINE.concat(prefixSpace).concat(context.getString("config_code_template_getter").replace("${field_name}", fieldName).replace("\r\n", "\r\n" + prefixSpace));
        String setCodeTemp = StringUtils.LINE.concat(prefixSpace).concat(context.getString("config_code_template_setter").replace("${field_name}", fieldName).replace("\r\n", "\r\n" + prefixSpace));
        getterSetterBuilder
                .append(enableCodeTemplate ? getCodeTemp : "")
                .append(StringUtils.LINE).append(prefixSpace).append("public ").append(fieldType).append(" get").append(StringUtils.upperCaseFirstChar(fieldName)).append("() {").append(StringUtils.LINE)
                .append(prefixSpace).append(StringUtils.PREFIX_SPACE).append("return this.").append(fieldName).append(";").append(StringUtils.LINE)
                .append(prefixSpace).append("}").append(StringUtils.LINE)
                .append(enableCodeTemplate ? setCodeTemp : "")
                .append(StringUtils.LINE).append(prefixSpace).append("public void set").append(StringUtils.upperCaseFirstChar(fieldName)).append("(").append(fieldType).append(" ").append(fieldName).append(") {").append(StringUtils.LINE)
                .append(prefixSpace).append(StringUtils.PREFIX_SPACE).append("this.").append(fieldName).append(" = ").append(fieldName).append(";").append(StringUtils.LINE)
                .append(prefixSpace).append("}").append(StringUtils.LINE);
    }

    public boolean isListFieldType() {
        return fieldType.equals("List") || fieldType.startsWith("List<");
    }

}
